package com.tj.collection.utils;


import com.fqgj.exception.common.ApplicationException;
import com.fqgj.log.factory.LogFactory;
import com.fqgj.log.interfaces.Log;
import com.qianli.cashmama.product.facade.AppFacade;
import com.qianli.cashmama.product.facade.result.AppPackageInfo;
import com.tj.collection.enums.MessageBizCodeEnum;
import com.tj.collection.enums.MessageSceneEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 马甲包 消息工具类
 *
 * @author moss
 * 2019-04-02
 */
@Component
public class MessageUtils {

    public static final Log LOGGER = LogFactory.getLog(MessageUtils.class);

    @Autowired
    ConfigUtil apolloConfigUtil;

    @Autowired
    private DbCacheConfig config; //inject config for namespace application

    @Autowired
    private AppFacade appFacade;

    /**
     * 查询用户下单app包信息
     *
     * @param userCode
     * @param tradeNo
     * @return
     */
    public AppPackageInfo getAppPackageInfoByUserCode(String userCode, String tradeNo) {
        try {
            return new AppPackageInfo();
        } catch (Exception e) {
            LOGGER.error("查询失败,userCode:{}", userCode, e);
            throw new ApplicationException("查询App信息失败");
        }
    }

    /**
     * 根据用户code 和 消息场景确定 消息bizCode
     */
    public String getMessageBizCodeByPackageName(String userCode, MessageSceneEnum messageSceneEnum) {
        AppPackageInfo appPackageInfo = this.getAppPackageInfoByUserCode(userCode, null);

        String appCode = appPackageInfo.getAppCode();
        String appName = appPackageInfo.getAppName();
        String packageName = appPackageInfo.getPackageName();

        //已经在运行的app 还是按照appName获取场景码
        if ("in.cashmama.app".equals(packageName) || "in.dhanadhan.loan.money.cash".equals(packageName)
                || "in.credfox.lending".equals(packageName)
                || "35".equals(appCode) || "36".equals(appCode)) {
            return MessageBizCodeEnum.getMessageBizCodeEnum(appName, messageSceneEnum);
        } else {
            return MessageBizCodeEnum.getMessageBizCodeByPackageName(packageName, messageSceneEnum);
        }
    }

    /**
     * 根据用户code 和 消息场景确定 消息bizCode
     * 优先根据trade包名查询
     *
     * @param userCode
     * @param tradeNo
     * @param messageSceneEnum
     * @return
     */
    public String getMessageBizCodeByPackageName(String userCode, String tradeNo, MessageSceneEnum messageSceneEnum) {
        AppPackageInfo appPackageInfo = this.getAppPackageInfoByUserCode(userCode, tradeNo);

        String appCode = appPackageInfo.getAppCode();
        String appName = appPackageInfo.getAppName();
        String packageName = appPackageInfo.getPackageName();

        //已经在运行的app 还是按照appName获取场景码
        String messageBizCode = null;
        if ("in.cashmama.app".equals(packageName) || "in.dhanadhan.loan.money.cash".equals(packageName)
                || "in.credfox.lending".equals(packageName)
                || "35".equals(appCode) || "36".equals(appCode)) {
            messageBizCode = MessageBizCodeEnum.getMessageBizCodeEnum(appName, messageSceneEnum);
        } else {
            messageBizCode = MessageBizCodeEnum.getMessageBizCodeByPackageName(packageName, messageSceneEnum);
        }
        LOGGER.info("admin获取场景码,userCode:{},tradeNo:{},packageName:{},messageBizCode:{}", userCode, tradeNo, packageName, messageBizCode);
        return messageBizCode;
    }

    /**
     * 获取邮件模板
     */
    public String getMailBasicTemplateUrl(String userCode, String tradeNo) {
        AppPackageInfo appPackageInfo = this.getAppPackageInfoByUserCode(userCode, tradeNo);

        String appCode = appPackageInfo.getAppCode();
        String appName = appPackageInfo.getAppName();
        String packageName = appPackageInfo.getPackageName();

        //已经在运行的app 还是按照appName获取场景码
        String key = null;
        if ("in.cashmama.app".equals(packageName) || "in.dhanadhan.loan.money.cash".equals(packageName)
                || "in.credfox.lending".equals(packageName)
                || "35".equals(appCode) || "36".equals(appCode)) {
            key = String.format("%s.mail.basic.template.url", appName.toLowerCase());
        } else {
            key = String.format("%s.mail.basic.template.url", packageName.replaceAll("\\.", "").toLowerCase());
        }
        String template = config.getProperty(key, "https://cashmama-pro.oss-ap-south-1.aliyuncs.com/email/basicTemplate.html");
        LOGGER.info("admin获取邮件模板,userCode:{},tradeNo:{},key:{},template:{}", userCode, tradeNo, key, template);
        return template;
    }

    /**
     * 获取邮件模板02
     */
    public String getMailBasicTemplate02Url(String userCode, String tradeNo) {
        AppPackageInfo appPackageInfo = this.getAppPackageInfoByUserCode(userCode, tradeNo);

        String appCode = appPackageInfo.getAppCode();
        String appName = appPackageInfo.getAppName();
        String packageName = appPackageInfo.getPackageName();

        //已经在运行的app 还是按照appName获取场景码
        String key = null;
        if ("in.cashmama.app".equals(packageName) || "in.dhanadhan.loan.money.cash".equals(packageName)
                || "in.credfox.lending".equals(packageName)
                || "35".equals(appCode) || "36".equals(appCode)) {
            key = String.format("%s.mail.basic.template_2.url", appName.toLowerCase());
        } else {
            key = String.format("%s.mail.basic.template_2.url", packageName.replaceAll("\\.", "").toLowerCase());
        }
        return config.getProperty(key, "https://cashmama-pro.oss-ap-south-1.aliyuncs.com/email/basicTemplate_2.html");
    }
}
